package com.rs.controller;

import java.awt.EventQueue;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import com.github.pagehelper.Page;
import com.rs.model.ZhUser;
import com.rs.model.ZhWorklog;
import com.rs.service.ZhUserService;
import com.rs.service.ZhUserTeacherService;
import com.rs.service.ZhWorklogService;
import com.rs.util.DateChooserJButton;
import com.rs.util.DateFormatUtil;
import com.rs.util.SpringContextUtils;

import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionEvent;
import javax.swing.BoxLayout;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.Font;
import javax.swing.event.AncestorListener;
import javax.swing.event.AncestorEvent;

public class TeacherWorkLog extends JInternalFrame {
	private JTable table;
	
	private Object [][]data=null;
	
	private String head[]=null;
	
	//首页
	private Integer pageNum =1;
	
	//总页数
	private Integer pages;
	
	
	private ZhWorklogService zhWorklogService = SpringContextUtils.getBean(ZhWorklogService.class);

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TeacherWorkLog frame = new TeacherWorkLog();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TeacherWorkLog() {
		setClosable(true);
		setMaximizable(true);
		setIconifiable(true);
		setResizable(false);
		setTitle("工作日志列表");
		setBounds(100, 100, 531, 471);
		getContentPane().setLayout(null);
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 76, 505, 302);
		getContentPane().add(scrollPane);
		
		table = new JTable();
		table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		head=new String[] {
		            "ID编号", "教师姓名", "发布时间", "发布内容",
		        };
		
		table.setModel(new DefaultTableModel(queryData(pageNum,null),head));
		
		scrollPane.setViewportView(table);
		table.addMouseListener(new MouseAdapter()
		{
			public void mouseClicked(MouseEvent e) {
				int row =table.rowAtPoint(e.getPoint());// 选中行
				ZhWorklog zhWorklog = new ZhWorklog();
				zhWorklog.setWlId((int)table.getValueAt(row, 0));
				new TeacherWorkLogText(zhWorklogService.selectByPrimaryKey(zhWorklog)).setVisible(true);
			}
		});
		
		
		JPanel panel = new JPanel();
		panel.setBounds(0, 0, 515, 77);
		getContentPane().add(panel);
		panel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("教师名称：");
		lblNewLabel.setBounds(29, 13, 66, 30);
		panel.add(lblNewLabel);
		
		final JComboBox comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(getTeacherData()));
		comboBox.setBounds(105, 18, 78, 21);
		panel.add(comboBox);
		
		JLabel label = new JLabel("时间日期：");
		label.setBounds(221, 13, 66, 30);
		panel.add(label);
		
		final DateChooserJButton btnNewButton_4 = new DateChooserJButton();
		btnNewButton_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SpinnerDateModel model = new SpinnerDateModel();
			    JSpinner year = new JSpinner(model);
				year.setValue(new Date());
		        JSpinner.DateEditor editor = new JSpinner.DateEditor(year,"yyyy-MM-dd");
		        year.setEditor(editor);
		        year.setBounds(402, 298, 50, 27);
			}
		});
		
		btnNewButton_4.setBounds(297, 17, 93, 23);
		panel.add(btnNewButton_4);
		
		JButton btnNewButton = new JButton("查询");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ZhWorklog zkWorklog = new ZhWorklog();
				zkWorklog.setWlTime(new DateFormatUtil().getDateString(btnNewButton_4.getDate(),"yyyy-MM-dd")  );
				if(!comboBox.getSelectedItem().equals("无")){
					zkWorklog.setU_name((String)comboBox.getSelectedItem());
				}
				SetTheValue(zkWorklog);
			}
		});
		btnNewButton.setBounds(412, 17, 93, 23);
		panel.add(btnNewButton);
		
		JButton btnNewButton_5 = new JButton("刷新");
		btnNewButton_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				SetTheValue(null);
			}
		});
		btnNewButton_5.setBounds(412, 50, 93, 23);
		panel.add(btnNewButton_5);
		
		
		
		JPanel panel_1 = new JPanel();
		panel_1.setBounds(0, 378, 515, 56);
		getContentPane().add(panel_1);
		panel_1.setLayout(null);
		
		JButton btnNewButton_1 = new JButton("首页");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pageNum=1;
				ZhWorklog zkWorklog = new ZhWorklog();
				zkWorklog.setWlTime(new DateFormatUtil().getDateString(btnNewButton_4.getDate(),"yyyy-MM-dd")  );
				if(!comboBox.getSelectedItem().equals("无")){
					zkWorklog.setU_name((String)comboBox.getSelectedItem());
				}
				SetTheValue(zkWorklog);
			}
		});
		btnNewButton_1.setBounds(10, 0, 93, 23);
		panel_1.add(btnNewButton_1);
		
		JButton btnNewButton_2 = new JButton("上一页");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				pageNum-=1;
				// 判断是否是第一页				
				if(pageNum < 1) {
					showMessageDialog("已是第一页！");
					pageNum=1;
				}else {
					ZhWorklog zkWorklog = new ZhWorklog();
					zkWorklog.setWlTime(new DateFormatUtil().getDateString(btnNewButton_4.getDate(),"yyyy-MM-dd")  );
					if(!comboBox.getSelectedItem().equals("无")){
						zkWorklog.setU_name((String)comboBox.getSelectedItem());
					}
					SetTheValue(zkWorklog);
				}
			}
		});
		btnNewButton_2.setBounds(210, 0, 93, 23);
		panel_1.add(btnNewButton_2);
		
		JButton btnNewButton_3 = new JButton("下一页");
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 判断是否是最后一页				
				if(pageNum == pages) {
					showMessageDialog("已是最后一页！");
				}else {
					pageNum+=1;
					ZhWorklog zkWorklog = new ZhWorklog();
					zkWorklog.setWlTime(new DateFormatUtil().getDateString(btnNewButton_4.getDate(),"yyyy-MM-dd")  );
					if(!comboBox.getSelectedItem().equals("无")){
						zkWorklog.setU_name((String)comboBox.getSelectedItem());
					}
					SetTheValue(zkWorklog);
				}
			}
		});
		btnNewButton_3.setBounds(349, 0, 93, 23);
		panel_1.add(btnNewButton_3);
	}
	
	//提示弹窗
	public void showMessageDialog(String massage){
		JOptionPane.showMessageDialog(this,massage);
	}
	
	//清空重新渲染数据
	public void SetTheValue(ZhWorklog record){
		TableModel tb=table.getModel();
		listNull(tb,10,4);
		table.setModel(new DefaultTableModel(queryData(pageNum,record),head));
	}
	
	//清空数据
	public void listNull(TableModel tb,int row,int cou){
		 for(int i=0;i<row;i++){
			for(int j=0;j<cou;j++){
				tb.setValueAt(null, i, j);
			}
		 }
	}
	
	 //生成表格数据
    /**
     * @return
     */
    public Object[][] queryData(Integer pageNum,ZhWorklog record){
        List<ZhWorklog> list= getData(pageNum, record);
        data = new Object[10][head.length];
        for(int i=0;i<10;i++){
            for(int j=0;j<head.length;j++){
            	if(i>list.size()-1) {
            		 data[i][0]=null;
                     data[i][1]=null;
                     data[i][2]=null;
                     data[i][3]=null;
            	}else {
            		 data[i][0]=list.get(i).getWlId();
                     data[i][1]=list.get(i).getU_name();
                     data[i][2]=list.get(i).getWlTime();
                     data[i][3]=list.get(i).getWlText();
            	}
            }
        }
        return data;
    }
	//获取工作日志数据
	public List<ZhWorklog> getData(Integer pageNum,ZhWorklog record){
		Page<ZhWorklog> dataList =	zhWorklogService.selectByZhWorklogList(pageNum, 10, record);
		pages = dataList.getPages();
		//获取数据 
		List<ZhWorklog> data =	new ArrayList<ZhWorklog>();
		for (ZhWorklog zhWorklog : dataList.getResult()) {
			data.add(zhWorklog);
		}
		return data;
	}
	//获取所有教师的姓名
	public String[] getTeacherData(){
		ZhUser user = new ZhUser();
		user.setuPoid(2);
		Page<ZhUser> dataList = SpringContextUtils.getBean(ZhUserTeacherService.class).selectTeachers(user);
		List<ZhUser> data = new ArrayList<ZhUser>();
		for (ZhUser zhUser : dataList.getResult()) {
			data.add(zhUser);
		}
		String[] typeTeacherStrings =  new String[data.size()+1];
		typeTeacherStrings[0] = "无";
		for (int i = 1; i <= data.size(); i++) {
			typeTeacherStrings[i] = data.get(i-1).getuName();
		}
		return typeTeacherStrings;
	}
}
